import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_background_service_ohos/flutter_background_service_ohos.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:mockito/mockito.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';

class MockFlutterBackgroundServiceOhos extends Mock
    implements FlutterBackgroundServiceOhos {}

class MockFlutterLocalNotificationsPlugin extends Mock
    implements FlutterLocalNotificationsPlugin {}

void main() {
  late MockFlutterBackgroundServiceOhos mockService;
  late MockFlutterLocalNotificationsPlugin mockNotifications;

  setUp(() {
    mockService = MockFlutterBackgroundServiceOhos();
    mockNotifications = MockFlutterLocalNotificationsPlugin();
  });

  /// Test the initialization of the service.
  testWidgets('Test service initialization', (WidgetTester tester) async {
    when(mockService.getPlatformVersion()).thenAnswer((_) async => 'Ohos 10');

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: Center(
            child: Builder(
              builder: (context) {
                return ElevatedButton(
                  onPressed: () async {
                    await mockService.getPlatformVersion();
                  },
                  child: const Text('Get Platform Version'),
                );
              },
            ),
          ),
        ),
      ),
    );

    await tester.tap(find.byType(ElevatedButton));
    await tester.pump();

    verify(mockService.getPlatformVersion()).called(1);
  });

  /// Test the notification functionality.
  testWidgets('Test notification sending', (WidgetTester tester) async {
    when(mockNotifications.show(
      888,
      'COOL SERVICE',
      any,
      any,
    )).thenAnswer((_) async => true);

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: Center(
            child: Builder(
              builder: (context) {
                return ElevatedButton(
                  onPressed: () async {
                    await mockNotifications.show(
                      888,
                      'COOL SERVICE',
                      'Awesome ${DateTime.now()}',
                      const NotificationDetails(
                        ohos: OhosNotificationDetails(
                          OhosNotificationSlotType.SERVICE_INFORMATION,
                          icon: '',
                          ongoing: true,
                        ),
                      ),
                    );
                  },
                  child: const Text('Send Notification'),
                );
              },
            ),
          ),
        ),
      ),
    );

    await tester.tap(find.byType(ElevatedButton));
    await tester.pump();

    verify(mockNotifications.show(
      888,
      'COOL SERVICE',
      any,
      any,
    )).called(1);
  });

  /// Test error handling for platform version retrieval.
  testWidgets('Test platform version error handling', (WidgetTester tester) async {
    when(mockService.getPlatformVersion()).thenThrow(PlatformException(code: 'ERROR'));

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: Center(
            child: Builder(
              builder: (context) {
                return ElevatedButton(
                  onPressed: () async {
                    try {
                      await mockService.getPlatformVersion();
                    } catch (e) {
                      // Handle error
                    }
                  },
                  child: const Text('Get Platform Version'),
                );
              },
            ),
          ),
        ),
      ),
    );

    await tester.tap(find.byType(ElevatedButton));
    await tester.pump();

    verify(mockService.getPlatformVersion()).called(1);
  });
}